#!/bin/bash

# 测试脚本：订单和哈希链功能

echo "=== 订单API测试 ==="

# 1. 创建测试订单
echo "1. 创建测试订单..."
curl -X POST http://localhost:8080/api/orders/test \
  -H "Content-Type: application/json" \
  -d '{}' | jq
echo

# 从响应中提取订单ID（需要手动替换）
ORDER_ID=1

# 2. 创建新订单
echo "2. 创建新订单..."
curl -X POST http://localhost:8080/api/orders \
  -H "Content-Type: application/json" \
  -d '{
    "orderNo": "ORD-001",
    "amount": 299.99,
    "status": 1
  }' | jq
echo

# 3. 获取订单详情
echo "3. 获取订单详情..."
curl -X GET http://localhost:8080/api/orders/$ORDER_ID \
  -H "Content-Type: application/json" | jq
echo

# 4. 更新订单
echo "4. 更新订单..."
curl -X PUT http://localhost:8080/api/orders/$ORDER_ID \
  -H "Content-Type: application/json" \
  -d '{
    "orderNo": "ORD-001-UPDATED",
    "amount": 399.99,
    "status": 2
  }' | jq
echo

# 5. 再次更新订单
echo "5. 再次更新订单..."
curl -X PUT http://localhost:8080/api/orders/$ORDER_ID \
  -H "Content-Type: application/json" \
  -d '{
    "orderNo": "ORD-001-CONFIRMED",
    "amount": 399.99,
    "status": 2
  }' | jq
echo

# 6. 获取订单的所有版本历史
echo "6. 获取订单的所有版本历史..."
curl -X GET http://localhost:8080/api/orders/$ORDER_ID/versions \
  -H "Content-Type: application/json" | jq
echo

# 7. 获取订单的最新版本
echo "7. 获取订单的最新版本..."
curl -X GET http://localhost:8080/api/orders/$ORDER_ID/latest-version \
  -H "Content-Type: application/json" | jq
echo

echo "=== 测试完成 ==="